package com.zijie.hosptal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.common.Result;
import com.zijie.hosptal.pojo.po.Reserve;
import com.zijie.hosptal.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/reserve")
public class ReserveController {
    private ReserveService reserveService;

    @Autowired
    public void setReserveService(ReserveService reserveService) {
        this.reserveService = reserveService;
    }
    @GetMapping
    public Result<?> getReserveList(Integer page, Integer limit, Reserve reserve,
                                    @RequestParam(required = false) Integer scheduleId
                                    ) {

//        System.out.println(scheduleId);
        if (scheduleId != null){
            reserve.setScheduleId(scheduleId);
        }
        Page<Reserve> reservePage = Page.of(page, limit);
        reservePage = reserveService.list(reservePage, reserve);
        return Result.success(reservePage);
    }

    @GetMapping("/searchReserves")
    public Result<?> searchReserves(Integer  page, Integer limit, Reserve reserve) {
        Page<Reserve> reservePage=Page.of(page, limit);
        reservePage = reserveService.listReserves(reservePage, reserve);
        return Result.success(reservePage);
    }

    @PostMapping
    public Result<?> addReserve(@RequestBody Reserve reserve) {
        boolean save = reserveService.save(reserve);
        if (save) {
            return Result.success("添加成功");
        }
        return Result.fail("已过预约日期无法预约");
    }
    //修改
    @PutMapping
    public Result<?> updateReserve(@RequestBody Reserve reserve) {
        boolean flag = reserveService.updateById(reserve);
        return flag ? Result.success() : Result.fail("预约已经截止");
    }
    //评分
    @PutMapping("/rate")
    public Result<?> rateReserve(@RequestBody Reserve reserve) {
        boolean flag = reserveService.rateReserve(reserve);
        return flag ? Result.success() : Result.fail("评分失败");
    }
    //批量操作
    @PutMapping("/batchSign")
    public Result<?> batch(@RequestBody List<Reserve> list) {
        boolean flag = reserveService.saveOrUpdateBatch(list);
        return flag ? Result.success() : Result.fail("批量操作失败");
    }
    //取消 预约
    @PutMapping("/cancel")
    public Result<?> cancelReserve(@RequestBody Reserve reserve) {
        boolean flag = reserveService.removeReserve(reserve);
        return flag ? Result.success() : Result.fail("取消失败");
    }
    //邮件
    @PostMapping("/sendEmail")
    public Result<?> sendEmail(@RequestBody Map<String, Object> requestData) {
        Integer patientId = (Integer) requestData.get("patientId");
        String content = (String) requestData.get("content");
        boolean flag = reserveService.sendEmail(patientId, content);
        return flag ? Result.success() : Result.fail("发送邮件失败");
    }

}
